import App from './App'


import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'
 import share from '@/utils/share.js'        // 导入并挂载全局的分享方法
 Vue.mixin(share)
Vue.prototype.$showLoading = () => {
	uni.showLoading({
		mask: true
	})
	setTimeout(function () {
		uni.hideLoading();
	}, 10000);
}

Vue.prototype.$onLaunched = new Promise(resolve => {
	Vue.prototype.$isResolve = resolve
})

Vue.prototype.$baseUrl = 'https://apiv1.miaomiaohf.store/api'
Vue.prototype.$http = (url, data, method, ct) => {
	return new Promise(resolve => {
		let form = {
			'Content-Type': 'application/json'
		}
		if (ct) {
			form = {
				'Content-Type': 'application/x-www-form-urlencoded',
			}
		}
		uni.request({
			url: Vue.prototype.$baseUrl + url,
			method: method || 'GET',
			data: data || {},
			header: {
				...form,
				Authorization: uni.getStorageSync('token') ? 'Bearer ' + uni.getStorageSync(
					'token') : '',
					noauth:'112233'
			},
			success: (res) => {
				if (res.statusCode == 200) {
					resolve(res.data);
				}
			}
		});
	})
}
Vue.prototype.$uploadImage = (item) => {
	return new Promise(resolve => {
		// console.log(item,"??/*  */");
		uni.uploadFile({
			url: Vue.prototype.$baseUrl + '/v1/upload/image',
			filePath: item,
			header: {
				Authorization: uni.getStorageSync('token') ? 'Bearer ' + uni.getStorageSync(
					'token') : ''
			},
			name: 'file',
			formData: {
							'type': 'avatar'
						},
			success: (res1) => {
				console.log(res1);
				if (res1.statusCode == 200) {
					resolve(res1.data);
				}
			},
			fail: (res1) => {
				console.log(res1,"失败");
			},
			complete() {
				uni.hideLoading();
			}
		});
	})
}

Vue.prototype.$timeAgo = (dateTimeStamp, type) => {
	if (type == 1) {
		var datetime1 = new Date();
		datetime1.setTime(dateTimeStamp);
		console.log(datetime1.setTime(dateTimeStamp),dateTimeStamp);
		var Nyear1 = datetime1.getFullYear();
		var Nmonth1 = datetime1.getMonth() + 1 < 10 ? "0" + (datetime1.getMonth() + 1) : datetime1.getMonth() + 1;
		var Ndate1 = datetime1.getDate() < 10 ? "0" + datetime1.getDate() : datetime1.getDate();
		var Nhour1 = datetime1.getHours() < 10 ? "0" + datetime1.getHours() : datetime1.getHours();
		var Nminute1 = datetime1.getMinutes() < 10 ? "0" + datetime1.getMinutes() : datetime1.getMinutes();
		var Nsecond1 = datetime1.getSeconds() < 10 ? "0" + datetime1.getSeconds() : datetime1.getSeconds();
		var result1 = Nyear1 + "-" + Nmonth1 + "-" + Ndate1 + ' ' + Nhour1 + ':' + Nminute1 + ':' + Nsecond1
		return result1;
	}

	var minute = 1000 * 60; //把分，时，天，周，半个月，一个月用毫秒表示
	var hour = minute * 60;
	var day = hour * 24;
	var week = day * 7;
	var halfamonth = day * 15;
	var month = day * 30;
	var now = new Date().getTime(); //获取当前时间毫秒
	var result = '';
	var diffValue = now - dateTimeStamp; //时间差

	if (diffValue < 0) {
		return "刚刚";
	}
	var minC = diffValue / minute; //计算时间差的分，时，天，周，月
	var hourC = diffValue / hour;
	var dayC = diffValue / day;
	var weekC = diffValue / week;
	var monthC = diffValue / month;
	if (monthC >= 1 && monthC <= 3) {
		result = " " + parseInt(monthC) + "月前"
	} else if (weekC >= 1 && weekC <= 3) {
		result = " " + parseInt(weekC) + "周前"
	} else if (dayC >= 1 && dayC <= 6) {
		result = " " + parseInt(dayC) + "天前"
	} else if (hourC >= 1 && hourC <= 24) {
		result = " " + parseInt(hourC) + "小时前"
	} else if (minC >= 1 && minC <= 59) {
		result = " " + parseInt(minC) + "分钟前"
	} else if (diffValue >= 0 && diffValue <= minute) {
		result = "刚刚"
	} else {
		var datetime = new Date();
		datetime.setTime(dateTimeStamp);
		var Nyear = datetime.getFullYear();
		var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
		var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
		var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
		var Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
		var Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
		result = Nyear + "-" + Nmonth + "-" + Ndate
	}
	return result;
}
Vue.prototype.$showToast = (info,iconTxt='none') => {
	uni.showToast({
		title: info,
		icon:iconTxt,
		duration:1500
	})
}
Vue.prototype.$generateUUID = () => {
	let d = new Date().getTime()
	const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		const r = (d + Math.random() * 16) % 16 | 0
		d = Math.floor(d / 16)
		return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16)
	})
	return uuid
}

// Vue.prototype.$completeUrl = (url) => {
	// 注册全局过滤器
Vue.filter('completeUrl', function(url) {
	  if (!url) return ''; // 处理空值情况
	let StaticUrl = 'https://apiv1.miaomiaohf.store/static_shoperv4/';
	if (url.startsWith('https://') || url.startsWith(
			'http://') || url.startsWith('//')) {
		return url
	} else {
		return StaticUrl + url
	}
})
uni.setStorageSync('version', '1.0.0')
import navbarMixin from '@/mixins/navbarMixin'

// 全局混入
Vue.mixin(navbarMixin)
const app = new Vue({
	...App
})
app.$mount()